
Imports System.Data.SqlClient
Imports System.Xml
Imports System.IO.StreamWriter




Public Class FrmConsulta


    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim GeoConsulta As New GeoWayPoints
        Dim Punto As New GeoPoint

        Dim Fecha As String
        Dim HoraMin As String
        Dim HoraMax As String


        If DataGridView2.Rows.Count > 0 Or DataGridView2.Columns.Count > 0 Then
            BorrarDataGridDatos()
        End If


        Fecha = DateTimePicker1.Value.ToString("dd/MM/yy")
        HoraMin = NumericUpDownHoraInicio.Value.ToString("00") + ":" + NumericUpDownMinutoInicia.Value.ToString("00") + ":00"
        HoraMax = NumericUpDownHoraFinaliza.Value.ToString("00") + ":" + NumericUpDownMinutoFinaliza.Value.ToString("00") + ":00"

        Dim Data As DataSet = GeoConsulta.FillPointsInWayPoint(Fecha, HoraMin, HoraMax)
        DataGridView2.DataSource = Data.Tables("PuntosW")

        DataGridView2.Columns.Add("Distancia", "Distancia")
        DataGridView2.Columns.Add("Velocidad", "Velocidad en KM/H")
        DataGridView2.Columns.Add("GeoCodingInverso", "GeoCodingInverso")


        DataGridView2.Columns(0).AutoSizeMode = DataGridViewAutoSizeColumnMode.ColumnHeader
        DataGridView2.Columns(1).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        DataGridView2.Columns(2).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        DataGridView2.Columns(3).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        DataGridView2.Columns(4).AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells

        DataGridView2.Columns("Velocidad").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        DataGridView2.Columns("Distancia").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells
        DataGridView2.Columns("GeoCodingInverso").AutoSizeMode = DataGridViewAutoSizeColumnMode.AllCells


        If Data.Tables("PuntosW").Rows.Count = 0 Then

            MsgBox("Esta consulta no tiene puntos para mostrar")
            Exit Sub

        End If


        'ConsultaWayPoints = GeoConsulta.FillPointsInWayPoint(Fecha, HoraMin, HoraMax)

        'For Each Punto In ConsultaWayPoints
        '    MsgBox(Punto.HoraUTC)
        'Next

        Dim TestingFileKML1 As String
        Dim TestingFileKML2 As String
        Dim TestingFileKML3 As String
        Dim TestingFileKML4 As String
        Dim TestingFileKML5 As String

        TestingFileKML1 = "<kml xmlns='http://www.opengis.net/kml/2.2'>" & vbCrLf
        TestingFileKML1 += "<Document>" & vbCrLf
        TestingFileKML1 += "<name>LineStyle.kml</name>" & vbCrLf
        TestingFileKML1 += "<open>1</open>" & vbCrLf
        TestingFileKML1 += "<Style id='linestyleExample'>" & vbCrLf
        TestingFileKML1 += "<LineStyle>" & vbCrLf
        TestingFileKML1 += "<color>7f0000ff</color>" & vbCrLf
        TestingFileKML1 += "<width>4</width>" & vbCrLf
        TestingFileKML1 += "</LineStyle>" & vbCrLf
        TestingFileKML1 += "</Style>" & vbCrLf
        TestingFileKML1 += "<Style id='red'><IconStyle><color>ff0000ff</color><scale>.3</scale></IconStyle></Style>" & vbCrLf
        TestingFileKML1 += "<Placemark>" & vbCrLf
        TestingFileKML1 += "<name>LineStyle Example</name>" & vbCrLf
        TestingFileKML1 += "<styleUrl>#linestyleExample</styleUrl>" & vbCrLf
        TestingFileKML1 += "<LineString>" & vbCrLf
        TestingFileKML1 += "<extrude>1</extrude>" & vbCrLf
        TestingFileKML1 += "<tessellate>1</tessellate>" & vbCrLf
        TestingFileKML1 += "<coordinates>" & vbCrLf

        TestingFileKML2 = " </coordinates>" & vbCrLf
        TestingFileKML2 += "  </LineString>" & vbCrLf
        TestingFileKML2 += "    </Placemark>" & vbCrLf

        TestingFileKML3 = "<Placemark>" & vbCrLf
        TestingFileKML3 += " <Point>" & vbCrLf
        TestingFileKML3 += "   <coordinates>" & vbCrLf

        TestingFileKML4 = "    </coordinates>" & vbCrLf
        TestingFileKML4 += " </Point>" & vbCrLf
        TestingFileKML4 += "</Placemark>" & vbCrLf

        TestingFileKML5 = "</Document>" & vbCrLf
        TestingFileKML5 += "</kml>" & vbCrLf

        Dim KmlFileExportaData As New IO.StreamWriter("C:\Inetpub\wwwroot\mapas\test.kml", False)

        KmlFileExportaData.WriteLine(TestingFileKML1)

        Dim GeoDatoToMap As String

        For i As Integer = 0 To Data.Tables("PuntosW").Rows.Count - 1

            Latitud = Trim(Data.Tables("PuntosW").Rows(i).Item(3))
            Longitud = Trim(Data.Tables("PuntosW").Rows(i).Item(4))

            GeoDatoToMap = "-" & Longitud & ",-" & Latitud

            KmlFileExportaData.WriteLine(GeoDatoToMap)

        Next

        KmlFileExportaData.WriteLine(TestingFileKML2)

        Dim NumeroPuntosINWayPoints As Integer = Data.Tables("PuntosW").Rows.Count
        Dim PuntosMostar As Integer = 10
        Dim Intervalo As Integer = NumeroPuntosINWayPoints / PuntosMostar

        For i As Integer = 0 To (NumeroPuntosINWayPoints - 1) Step Intervalo

            Latitud = Trim(Data.Tables("PuntosW").Rows(i).Item(3))
            Longitud = Trim(Data.Tables("PuntosW").Rows(i).Item(4))

            GeoDatoToMap = "-" & Longitud & ",-" & Latitud & ",0"

            KmlFileExportaData.WriteLine(TestingFileKML3)
            KmlFileExportaData.WriteLine(GeoDatoToMap)
            KmlFileExportaData.WriteLine(TestingFileKML4)

        Next


        KmlFileExportaData.WriteLine(TestingFileKML5)


        KmlFileExportaData.Close()


        WebBrowser1.Navigate("http://localhost/mapas/map7.php")
        WebBrowser1.Refresh()
        'WebBrowser1.Update()


        Dim CaminoPuntos As New List(Of GeoPoint)

        Dim Ref1 As New GeoPoint
        Dim Ref2 As New GeoPoint


        MsgBox(Data.Tables("PuntosW").Rows.Count.ToString)

        For i As Integer = 0 To Data.Tables("PuntosW").Rows.Count - 2

            Ref1.Latitud = Data.Tables("PuntosW").Rows(i).Item(3)
            Ref1.Longitud = Data.Tables("PuntosW").Rows(i).Item(4)

            Ref2.Latitud = Data.Tables("PuntosW").Rows(i + 1).Item(3)
            Ref2.Longitud = Data.Tables("PuntosW").Rows(i + 1).Item(4)

            Ref1.HoraUTC = Trim(Data.Tables("PuntosW").Rows(i).Item(2))     'Trim(DataGridView2.Item(2, i).Value)
            Ref2.HoraUTC = Trim(Data.Tables("PuntosW").Rows(i + 1).Item(2)) 'Trim(DataGridView2.Item(2, i + 1).Value)

            Dim Distancia As Double = DistanceBetweenTwoPoints(Ref1.Latitud, Ref1.Longitud, Ref2.Latitud, Ref2.Longitud)
            DataGridView2.Item(6, i + 1).Value = Distancia

            Dim Demora As Integer = DateDiff(DateInterval.Second, Convert.ToDateTime(Ref1.HoraUTC), Convert.ToDateTime(Ref2.HoraUTC))
            DataGridView2.Item(7, i + 1).Value = (Distancia / Demora) * 3.6

            If (Distancia / Demora) * 3.6 > 30 Then

                DataGridView2.Rows(i + 1).DefaultCellStyle.BackColor = Color.Red
                DataGridView2.Rows(i + 1).DefaultCellStyle.ForeColor = Color.White

            End If

            If (Distancia / Demora) * 3.6 < 4 Then

                DataGridView2.Rows(i + 1).DefaultCellStyle.BackColor = Color.Green
                DataGridView2.Rows(i + 1).DefaultCellStyle.ForeColor = Color.White

            End If

            DataGridView2.CurrentCell = DataGridView2(8, i + 1)
            DataGridView2.CurrentRow.Selected = True

            Try

                Dim GeoInv As String = GeoCodingInverso(Ref1.Latitud, Ref1.Longitud)
                Dim G() As String = Split(GeoInv, """")
                DataGridView2.Item(8, i + 1).Value = G(1) 'GeoInv

                My.Application.DoEvents()

            Catch ex As Exception

            End Try

        Next

    End Sub



    Public Function BorrarDataGridDatos() As Boolean

        DataGridView2.DataSource = Nothing

        For i As Integer = 0 To DataGridView2.Rows.Count - 1
            DataGridView2.Rows.Remove(DataGridView2.CurrentRow)
        Next
        For i As Integer = 0 To DataGridView2.Columns.Count - 1
            DataGridView2.Columns.RemoveAt(0)
        Next

        Return True
    End Function


    Private Sub FrmConsulta_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

        WebBrowser1.Navigate("http://localhost/mapas/map7.php")
        WebBrowser1.Refresh()

    End Sub

    Private Sub DataGridView2_CellContentClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellClick

        
        Dim FormularioConsultaMapa As New FrmMapa
        FormularioConsultaMapa.Latitud = DataGridView2.Item(3, e.RowIndex).Value
        FormularioConsultaMapa.Longitud = DataGridView2.Item(4, e.RowIndex).Value
        FormularioConsultaMapa.Show()
        FormularioConsultaMapa.BringToFront()

    End Sub

    Private Sub DataGridView2_CellContentClick_1(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles DataGridView2.CellContentClick

    End Sub
End Class